import { RouteLocationNormalized, RouteRecordRaw } from 'vue-router';
import { GlobalStore } from '@/stores/global';

export default function usePermission() {
	const globalStore = GlobalStore();
	return {
		accessRouter(route: RouteLocationNormalized | RouteRecordRaw) {
			// @ts-ignore
			return !route.meta?.requiresAuth;
		},
		findFirstPermissionRoute(_routers: any, role = 'admin') {
			const cloneRouters = [..._routers];
			while (cloneRouters.length) {
				const firstElement = cloneRouters.shift();
				if (
					firstElement?.meta?.roles?.find((el: string[]) => {
						return el.includes('*') || el.includes(role);
					})
				)
					return { name: firstElement.name };
				if (firstElement?.children) {
					cloneRouters.push(...firstElement.children);
				}
			}
			return null;
		},
		// You can add any rules you want
	};
}
